class Solution {
public:
    int num = 0;
    int val = 0;

    void _kthSmallest(TreeNode* root)
    {
        if(root == nullptr) return;

        _kthSmallest(root->left);

        if(--num == 0)
        {   
            val = root->val;
            return;
        } 

        _kthSmallest(root->right);
    }
    
    int kthSmallest(TreeNode* root, int k) 
    {
        num = k;
        _kthSmallest(root);

        return val;
    }
};